package band.wukong.practice.javase.core.thread.Scheduling;

import java.io.IOException;

/**
 * Desc: Java虚拟机采用抢占式调度模型，而不是分时调度模型，同时启动多个线程后，不能保证每个线程轮流获得均等的CPU时间片。
 * Author: sunjs
 * Date: 2009-11-5 11:22:26
 */
public class TestScheduling extends Thread {
    private static StringBuffer log = new StringBuffer();
    private static int count = 0;

    public void run() {
        for (int a = 0; a < 20; a++) {
            log.append(Thread.currentThread().getName() + ":" + a + "  ");
            if (++count % 10 == 0)
                log.append("\n");
        }
    }

    public static void main(String[] args) throws IOException, InterruptedException {
        TestScheduling machine1 = new TestScheduling();
        TestScheduling machine2 = new TestScheduling();
        machine1.setName("m1");
        machine2.setName("m2");
        machine1.start();
        machine2.start();
        while (machine1.isAlive() || machine2.isAlive()) {
            Thread.sleep(500);
        }
        System.out.println(log);
    }
}
